﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.IO;

namespace DesktopNotes
{
    /// <summary>
    /// Interaction logic for NotesWindow.xaml
    /// </summary>
    public partial class NotesWindow : Window
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindow(
         [MarshalAs(UnmanagedType.LPTStr)] string lpClassName,
         [MarshalAs(UnmanagedType.LPTStr)] string lpWindowName);
        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(
         IntPtr hWndChild,      // handle to window
         IntPtr hWndNewParent   // new parent window
         );

        public string FileName;

        public NotesWindow()
        {
            InitializeComponent();
            for (int i = 0; i < int.MaxValue; i++)
            {
                if (File.Exists(System.IO.Path.Combine(System.Environment.CurrentDirectory, i.ToString() + ".note")))
                {

                }
                else
                {
                    FileName = System.IO.Path.Combine(System.Environment.CurrentDirectory, i.ToString() + ".note");
                    break;
                }
            }
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;

            IntPtr hwndf = mainWindowPtr;
            IntPtr hwndParent = FindWindow("ProgMan", null);
            SetParent(hwndf, hwndParent);
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void NewTab_Click(object sender, RoutedEventArgs e)
        {
            NotesWindow newWindow = new NotesWindow();
            newWindow.Show();
        }

        private void ChangeTitle_Click(object sender, RoutedEventArgs e)
        {
            Input NewInput = new Input();
            NewInput.ShowDialog();
            Title.Content = NewInput.ReturnCode;

            TextRange textRange = new TextRange(RTBText.Document.ContentStart, RTBText.Document.ContentEnd);

            StreamWriter Output = new StreamWriter(FileName);
            Output.WriteLine(Title.Content.ToString());
            Output.WriteLine(textRange.Text);
            Output.Close();
        }

        private void RTBText_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextRange textRange = new TextRange(RTBText.Document.ContentStart, RTBText.Document.ContentEnd);

            StreamWriter Output = new StreamWriter(FileName);
            Output.WriteLine(Title.Content.ToString());
            Output.WriteLine(textRange.Text);
            Output.Close();
        }

        private void RichTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            TextRange textRange = new TextRange(RTBText.Document.ContentStart, RTBText.Document.ContentEnd);

            StreamWriter Output = new StreamWriter(FileName);
            Output.WriteLine(Title.Content.ToString());
            Output.WriteLine(textRange.Text);
            Output.Close();
        }

        private void RTBText_MouseUp(object sender, MouseButtonEventArgs e)
        {
            TextRange textRange = new TextRange(RTBText.Document.ContentStart, RTBText.Document.ContentEnd);

            StreamWriter Output = new StreamWriter(FileName);
            Output.WriteLine(Title.Content.ToString());
            Output.WriteLine(textRange.Text);
            Output.Close();
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            File.Delete(this.FileName);
            this.Close();
        }

    }

}
